/*
 * Copyright 2010 Groupware.cc All right reserved. This software is the confidential and proprietary information of
 * Groupware.cc ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Groupware.cc.
 */
package com.rome.syncml.service;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.rome.syncml.bean.Contact;
import com.rome.syncml.constant.Constant;
import com.rome.syncml.domain.SyncItem;
import com.rome.syncml.persistence.SyncItemMapper;
import com.rome.syncml.util.ContactUtil;

import net.fortuna.ical4j.model.*;
import net.fortuna.ical4j.vcard.VCard;
import net.fortuna.ical4j.vcard.VCardBuilder;

/**
 * 类ContactService.java的实现描述：TODO 类实现描述
 * 
 * @author 11 2011-10-29 下午05:30:04
 */
@Service
public class ContactService {

    @Resource
    private SyncItemMapper      syncItemMapper;

    private static final String allContract  = "all_contract";
    private static final String contractType = "contract";

    public void get(String content) {

    }

    /**
     * 查询联系人
     * 
     * @param userId
     * @param queryString
     * @return
     */
    public Map<String,Object> query(long userId, long sourceId) {
        // 得到原始数据
        List<SyncItem> items = syncItemMapper.selectAllItems(userId, sourceId, false, contractType);
        List<Contact> all = new ArrayList<Contact>(items.size()); 
        Map<String, Boolean> initial = new HashMap<String, Boolean>();
        for (SyncItem item : items) {
            Contact contact = ContactUtil.getContact(item.getContent());
            contact.setId(item.getId());
            if(!StringUtils.isEmpty(contact.getInitial())){
                initial.put(contact.getInitial().toUpperCase(), true);
            }
            all.add(contact);
        }
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("all", all);
        map.put("initial", initial);
        return map;

    }
    
    public Contact getContact(long id){
        SyncItem item = syncItemMapper.selectByPrimaryKey(id);
        if(item!=null){
            Contact contact = ContactUtil.getContact(item.getContent());
            contact.setId(item.getId());
            return contact;
        }
        return null;
    }
    
    public void update(Contact contact){
        SyncItem item = new SyncItem();
        item.setId(contact.getId());
        item.setType("contract");
        item.setContent(ContactUtil.getVcard(contact));
        syncItemMapper.updateByPrimaryKeySelective(item);
    }
    
    public void insert(Contact contact,long userId){
        SyncItem item = new SyncItem();
        item.setType("contract");
        item.setDeleted(false);
        item.setUserId(userId);
        item.setSourceId(Constant.SOURCE_ID);
        item.setFormat("VCARD");
        item.setAnchor(0L);
        item.setContent(ContactUtil.getVcard(contact));
        syncItemMapper.insertSelective(item);
    }
    
    /**
     * 删除页面
     * @param ids
     */
    public void delete(long[] ids){
        if(ids!=null){
            for(long id : ids){
                syncItemMapper.logicDelete(id);
            }
        }
        
    }

}
